<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="产品分类" prop="productCategory">
        <el-select v-model="queryParams.productCategory" placeholder="请选择产品分类" clearable size="small">
          <el-option
            v-for="dict in configCategoryOptions"
            :key="dict.categoryCode"
            :label="dict.categoryName"
            :value="dict.categoryCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="属性名称中文" prop="attrNameCn">
        <el-input
          v-model="queryParams.attrNameCn"
          placeholder="请输入属性名称中文"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性名称英文" prop="attrNameEn">
        <el-input
          v-model="queryParams.attrNameEn"
          placeholder="请输入属性名称英文"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:configAttrAtch:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:configAttrAtch:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:configAttrAtch:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:configAttrAtch:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configAttrAtchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="产品分类" align="center" prop="productCategory" :formatter="productCategoryFormat"/>
      <el-table-column label="属性字段" align="center" prop="attrField"/>
      <el-table-column label="属性名称中文" align="center" prop="attrNameCn"/>
      <el-table-column label="属性名称英文" align="center" prop="attrNameEn"/>
      <el-table-column label="属性类型" align="center" prop="attrType" :formatter="attrTypeFormat"/>
      <el-table-column label="属性长度" align="center" prop="attrLength"/>
      <el-table-column label="是否必填" align="center" prop="isRequired" :formatter="isRequiredFormat"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:configAttrAtch:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:configAttrAtch:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改附加属性维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-row>

          <el-col :span="12">
            <el-form-item label="产品分类" prop="productCategory">
              <el-select v-model="form.productCategory" :disabled="form.id!=null" placeholder="请选择产品分类" :style="{width: '100%'}">
                <el-option
                  v-for="dict in configCategoryOptions"
                  :key="dict.categoryCode"
                  :label="dict.categoryName"
                  :value="dict.categoryCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="属性字段" prop="attrField">
              <el-select v-model="form.attrField" :disabled="form.id!=null" placeholder="属性字段与t_product_temp_attr表字段对应" :style="{width: '100%'}">
                <el-option
                  v-for="dict in attrFieldOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="属性名称中文" prop="attrNameCn">
              <el-input v-model="form.attrNameCn" placeholder="请输入属性名称中文"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="属性名称英文" prop="attrNameEn">
              <el-input v-model="form.attrNameEn" placeholder="请输入属性名称英文"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="属性类型" prop="attrType">
              <el-select v-model="form.attrType" placeholder="请选择属性类型" :style="{width: '100%'}">
                <el-option
                  v-for="dict in attrTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="下拉框值" prop="attrTypeSelect" v-show="form.attrType==='select'">
              <el-input v-model="form.attrTypeSelect" placeholder='[{"label": "选项名称","value": "选项值"}]'/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="属性长度" prop="attrLength">
              <el-input v-model="form.attrLength" placeholder="请输入属性长度"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否必填" prop="isRequired">
              <el-select v-model="form.isRequired" placeholder="请选择是否必填" :style="{width: '100%'}">
                <el-option
                  v-for="dict in isRequiredOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" :style="{width: '100%'}">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            1、附加属性维护之前，请先进行<span style="font-size: 10pt;color: red;">【菜单：产品标题维护】</span>
            <br>
            2、附加属性维护时，请将<span style="font-size: 10pt;color: red;">属性字段</span>按顺序追加（attr0,attr1...）
            <br>
            3、确定保存时，同时会更新<span style="font-size: 10pt;color: red;">【菜单：产品标题维护】</span>附加字段，方便动态导出产品附加属性字段信息（非管理员无需了解）
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfigAttrAtch, getConfigAttrAtch, delConfigAttrAtch, addConfigAttrAtch, updateConfigAttrAtch, exportConfigAttrAtch } from "@/api/amz/configAttrAtch";
import { getConfigCategoryByDept } from "@/api/amz/configCategory";

export default {
  name: "ConfigAttrAtch",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 附加属性维护表格数据
      configAttrAtchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 产品类别列表-部门
      configCategoryOptions: [],
      // 属性字段（手动创建，没必要创建数据库字典）
      attrFieldOptions: [
        {dictLabel: "属性attr1",dictValue:"attr1"},
        {dictLabel: "属性attr2",dictValue:"attr2"},
        {dictLabel: "属性attr3",dictValue:"attr3"},
        {dictLabel: "属性attr4",dictValue:"attr4"},
        {dictLabel: "属性attr5",dictValue:"attr5"},
        {dictLabel: "属性attr6",dictValue:"attr6"},
        {dictLabel: "属性attr7",dictValue:"attr7"},
        {dictLabel: "属性attr8",dictValue:"attr8"},
        {dictLabel: "属性attr9",dictValue:"attr9"},
        {dictLabel: "属性attr10",dictValue:"attr10"},
        {dictLabel: "属性attr11",dictValue:"attr11"},
        {dictLabel: "属性attr12",dictValue:"attr12"},
        {dictLabel: "属性attr13",dictValue:"attr13"},
        {dictLabel: "属性attr14",dictValue:"attr14"},
        {dictLabel: "属性attr15",dictValue:"attr15"}
      ],
      // 是否必填字典
      isRequiredOptions: [],
      // 状态字典
      statusOptions: [],
      // 属性类型字典
      attrTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCategory: null,
        attrNameCn: null,
        attrNameEn: null,
        status: null,
      },
      // 表单参数
      form: {
        attrLength: 128
      },
      // 表单校验
      rules: {
        productCategory: [
          { required: true, message: "产品分类不能为空", trigger: "change" }
        ],
        attrField: [
          { required: true, message: "属性字段中文不能为空", trigger: "blur" }
        ],
        attrNameCn: [
          { required: true, message: "属性名称中文不能为空", trigger: "blur" }
        ],
        attrNameEn: [
          { required: true, message: "属性名称英文不能为空", trigger: "blur" }
        ],
        attrType: [
          { required: true, message: "属性类型不能为空", trigger: "change" }
        ],
        isRequired: [
          { required: true, message: "是否必填不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    /* 获取类目配置-部门*/
    this.getConfigCategoryByDept();

    this.getDicts("qc_attr_type").then(response => {
      this.attrTypeOptions = response.data;
    });
    this.getDicts("qc_is_boolean").then(response => {
      this.isRequiredOptions = response.data;
    });
    this.getDicts("amz_status").then(response => {
      this.statusOptions = response.data;
    });


  },
  methods: {
    /** 查询附加属性维护列表 */
    getList() {
      this.loading = true;
      listConfigAttrAtch(this.queryParams).then(response => {
        this.configAttrAtchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /* 获取类目配置-部门*/
    getConfigCategoryByDept(){
      getConfigCategoryByDept().then(response => {
        this.configCategoryOptions = response.data;
      });
    },
    // 产品分类字典翻译
    productCategoryFormat(row, column) {
      var categoryName = "";
      this.configCategoryOptions.forEach(obj =>{
        if (obj.categoryCode===row.productCategory){
          categoryName = obj.categoryName;
        }
      });
      return categoryName;
    },
    // 是否必填字典翻译
    isRequiredFormat(row, column) {
      return this.selectDictLabel(this.isRequiredOptions, row.isRequired);
    },
    // 属性类型字典翻译
    attrTypeFormat(row, column) {
      return this.selectDictLabel(this.attrTypeOptions, row.attrType);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCategory: null,
        attrField: null,
        attrNameCn: null,
        attrNameEn: null,
        attrType: null,
        attrLength: 128,
        isRequired: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加附加属性维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfigAttrAtch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改附加属性维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfigAttrAtch(this.form).then(response => {
              if (response.code===200){
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }else {
                this.msgError(response.msg);
              }

            });
          } else {
            addConfigAttrAtch(this.form).then(response => {
              if (response.code===200){
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除附加属性维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfigAttrAtch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有附加属性维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportConfigAttrAtch(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
